
export default {
  bind(el) {
    let startPosition = { x: 0, y: 0 };
    let dialogPosition = { x: 0, y: 0 };
    let dragging = false;

    const handleMouseDown = (e) => {

      // 将当前元素的 z-index 设置为最高
      // el.style.zIndex = getHighestZIndex() + 1;

      startPosition.x = e.clientX;
      startPosition.y = e.clientY;
      dialogPosition.x = parseInt(el.style.left, 10) || 0;
      dialogPosition.y = parseInt(el.style.top, 10) || 0;

      document.addEventListener('mousemove', handleMouseMove);
      document.addEventListener('mouseup', handleMouseUp);

      dragging = true;

      e.stopPropagation();
      e.preventDefault();
    };

    const handleMouseMove = (e) => {
      if (dragging) {
        const offsetX = e.clientX - startPosition.x;
        const offsetY = e.clientY - startPosition.y;
        el.style.left = `${dialogPosition.x + offsetX}px`;
        el.style.top = `${dialogPosition.y + offsetY}px`;
      }
    };

    const handleMouseUp = () => {
      document.removeEventListener('mousemove', handleMouseMove);
      document.removeEventListener('mouseup', handleMouseUp);


      dragging = false;
    };

    el.addEventListener('mousedown', handleMouseDown);
  },
};

// 辅助函数：获取页面上最高的 z-index
function getHighestZIndex() {
  const elements = document.getElementsByTagName('*');
  let highestZIndex = 0;

  for (let i = 0; i < elements.length; i++) {
    const zIndex = parseInt(window.getComputedStyle(elements[i]).zIndex, 10);
    if (!isNaN(zIndex) && zIndex > highestZIndex) {
      highestZIndex = zIndex;
    }
  }

  return highestZIndex;
}


// 辅助函数：获取 .pop-pu 元素的 z-index
function getPopZIndex() {
  const popPu = document.querySelector('.pop-pu');
  if (popPu) {
    const zIndex = parseInt(window.getComputedStyle(popPu).zIndex, 10);
    return isNaN(zIndex) ? 0 : zIndex;
  }
  return 0;
}
